#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@File Name  : mp4_info_sql.py
@Author     : LeeCQ
@Date-Time  : 2019/12/6 17:16

通过下载链接获取Headers；
"""

import requests
import re
import os

# local
from SQL.MySQL.tencent_MySQL import TencentMySQL
# from MOOC.life_and_heart.local_file import LifeAndHeartLocalFileName


class LifeAndHeartDB(TencentMySQL):

    def __init__(self):
        super().__init__(user='root',
                         passwd='lichao187',
                         db='test',
                         )
        self.set_prefix('MOOC_')
        self.table_name = '人生与人心'

    def set_tableName(self, table_name):
        self.table_name = table_name

    def print_pre(self):
        print(self.TABLE_PREFIX + self.table_name)

    def create_life_and_heart(self):
        """"""
        command = (f"CREATE TABLE `{self.TABLE_PREFIX}{self.table_name}` (  "
                   "ID          VARCHAR(3)      UNIQUE, "
                   "Name        VARCHAR(100)            ,  "
                   "Page_Link   VARCHAR(103)            , "
                   "Src_Link    CHAR(71)                , "
                   "MediumData  LONGBLOB"
                   ")"
                   )
        return self.write_db(command)

    def insert_data(self, table, id_, name, src_link, page_link):
        """"""
        self.create_life_and_heart()
        return self.insert(table, ID=id_, Name=name, SRC_Link=src_link, Page_Link=page_link)


class LifeAndHeartMp4Name:
    """
    通过MP4下载链接获取到视频名称。
    """

    def __init__(self):
        """接口函数"""
        self.local_mp4_path = r'C:\Users\LCQ\Downloads\Video\lifeAndHeart\\'
        self.links = tuple(open('./sup/src_links.txt', 'r', encoding='utf8').read().split('\n'))
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400'
        }

    def get_a_name(self, url):
        """"""
        res = requests.head(url, headers=self.headers, timeout=2)
        print(res.headers)

    def insert_data(self):
        """向数据库插入数据"""

        page_link, id_, name = [x for x in zip(*self.get_page_link())]
        src_link = self.get_src_link()
        print(page_link, id_, name, src_link)
        with open('./sup/all.txt', 'w', encoding='utf8') as f:
            x = ['\t'.join(x) for x in zip(id_, name, page_link, src_link)]
            f.write('\n'.join(x))
        # _sql = LifeAndHeartDB()
        # _sql.insert_data(table=_sql.table_name,
        #                  id_=id_,
        #                  name=name,
        #                  page_link=page_link,
        #                  src_link=src_link
        #                  )

    def get_page_link(self):
        """包含 page_link, id, name"""
        with open('./sup/listURL.txt', 'r', encoding='utf8') as f:
            return [line.split('\t') for line in f.read().split('\n') if line != '']

    @staticmethod
    def get_src_link():
        with open('./sup/src_links.txt') as f:
            return f.read().split('\n')

    def get_id(self):
        """从文件名获取ID"""
        return [re.findall(r'([\d]\.[\d])', x)[0] for x in os.listdir(self.local_mp4_path)]  # 获取ID

    def get_name(self):
        return [x for x in os.listdir(self.local_mp4_path) if not len(x) == 1]


if __name__ == '__main__':
    # LifeAndHeartDB().print_pre()
    # LifeAndHeartMp4Name().get_a_name('http://d0.ananas.chaoxing.com/download/5106d8e50a6d2b0d7c463c89cdb7d2e9')
    LifeAndHeartMp4Name().insert_data()
